#!/usr/bin/env python
# -- coding: utf-8 --
"""
Copyright (c) 2018. All rights reserved.
Created by C. L. Wang on 2018/4/18

参考: https://juejin.im/post/5acfef976fb9a028db5918b5
"""

import numpy as np


def prp_2_oh_array(arr):
    """
    概率矩阵转换为OH矩阵
    arr = np.array([[0.1, 0.5, 0.4], [0.2, 0.1, 0.6]])
    :param arr: 概率矩阵
    :return: OH矩阵
    """
    arr_size = arr.shape[1]  # 类别数
    arr_max = np.argmax(arr, axis=1)  # 最大值位置
    oh_arr = np.eye(arr_size)[arr_max]  # OH矩阵
    return oh_arr


def is_same_line(td_arr):
    """
    判断是否行值全相等
    :param td_arr: 二维矩阵
    :return: True含有值相等的行
    """
    row_max = np.max(td_arr, axis=1)
    row_min = np.min(td_arr, axis=1)

    same_row = (row_max == row_min)
    return np.any(same_row)


def check_error_features(features):
    """
    检查含有nan和全相等行等异常数据

    :param features: 二维特征矩阵
    :return: 是否含有错误数据
    """
    if np.isnan(features).sum() > 0:
        return True
    elif is_same_line(features):
        return True
    return False
